package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.QuestionnaireContentDao;
import cn.topevery.garden.entity.po.QuestionnaireContent;
import cn.topevery.garden.entity.query.QuestionnaireContentQuery;
import cn.topevery.garden.entity.vo.QuestionnaireContentVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.basis.entity.po.SysUser;
import hw.topevery.basis.runtime.UserRuntime;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 题目内容
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class QuestionnaireContentDaoImpl extends BaseDaoImpl<QuestionnaireContent> implements QuestionnaireContentDao {
    @Override
    public <P extends DbCsp> List<QuestionnaireContent> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<QuestionnaireContentVO> listPage(QuestionnaireContentQuery query) {
        String table = "t_questionnaire_content tqc";

        String columns = "tqc.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tqc.c_id desc");
        sqlQueryMate
                .where(true, "tqc.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, QuestionnaireContentVO.class);
    }

    @Override
    public QuestionnaireContent getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public void deleteQuestionnaireDetailContentByDetailId(Integer detailId, Integer userId) {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_questionnaire_content");
        sqlUpdateMate.setColumnValue(true, "c_db_status", 1)
                .setColumnValue(true, "c_db_update_id", userId)
                .setColumnValue(true, "c_db_update_date", LocalDateTime.now())
                .where(true, "c_detail_id", ScriptConditionEnum.Eq, detailId);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }

    @Override
    public void addQuestionnaireContent(List<QuestionnaireContent> contentList, Integer userId) {
        SysUser user = UserRuntime.getSysUser();
        for (QuestionnaireContent questionnaireContent : contentList) {
            questionnaireContent.setDbStatus(false);
            questionnaireContent.setCreateId(user.userId);
            questionnaireContent.setCreateTime(LocalDateTime.now());
            questionnaireContent.setDbUpdateDate(LocalDateTime.now());
            questionnaireContent.setDbUpdateId(userId);
            insert(user.userId, questionnaireContent);
        }
    }

    @Override
    public List<QuestionnaireContent> getContentListByDetailId(Integer detailId) {
        SqlQueryMate sqlQueryMate = new SqlQueryMate("t_questionnaire_content").setColumns("*");
        sqlQueryMate.where(true, "c_db_status", ScriptConditionEnum.Eq, 0)
                .where(detailId!=null, "c_detail_id", ScriptConditionEnum.Eq, detailId);
        return selectList(sqlQueryMate, QuestionnaireContent.class);
    }
}
